package com.zpl.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zpl.user.entity.User;
import com.zpl.user.mapper.UserMapper;
import com.zpl.user.service.UserService;
import com.zpl.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisTemplate<String, Serializable> redisCacheTemplate;



    @Override
    @CacheEvict(value="user",key="'list'")
    public boolean insert(User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", user.getUsername());
        List<User> users = userMapper.selectList(queryWrapper);
        if (users.size() > 0) {
            return false;
        }
        userMapper.insert(user);
//        RedisUtil.init(redisTemplate);
//        RedisUtil.del("userList");

        return true;
    }

    @Override
    @CacheEvict(value="user",key="'list'")
    public boolean delete(Long id) {
        if (userMapper.deleteById(id) > 0) {
            return true;
        }
        return false;
    }

    @Override
    @CacheEvict(value="user",key="'list'")
    public boolean update(User user) {
        if (userMapper.updateById(user) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean login(String username, String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        List<User> users = userMapper.selectList(queryWrapper);
        if (users.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
//    @Cacheable(value="user", key="'users_'+#username")
    public List<User> getUserByUsername(String username) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        System.out.println("1");
        List<User> list = userMapper.selectList(queryWrapper);
        return list;
    }

    @Override
//    @Cacheable(value="user")
    public List<User> getUserByUsername() {
        QueryWrapper queryWrapper = new QueryWrapper();
        System.out.println("1");
        List<User> list = userMapper.selectList(queryWrapper);
        return list;
    }


    @Override
    @Cacheable(value="user",key="'list'")
    public List<User> getALLUser() {
        QueryWrapper queryWrapper = new QueryWrapper();
        System.out.println("1");
        List<User> list = userMapper.selectList(queryWrapper);
        return list;
    }


}
